package cn.iocoder.yudao.module.biz.controller.admin.htPlanExe;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.controller.admin.htPlanDetail.vo.HtPlanDetailJoinPageVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.htPlanExe.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.htPlanExe.HtPlanExeDO;
import cn.iocoder.yudao.module.biz.convert.htPlanExe.BizHtPlanExeConvert;
import cn.iocoder.yudao.module.biz.service.htPlanExe.HtPlanExeService;

@Api(tags = "管理后台 - 排查计划执行任务")
@RestController
@RequestMapping("/biz/ht-plan-exe")
@Validated
public class HtPlanExeController {

    @Resource
    private HtPlanExeService htPlanExeService;

    @PostMapping("/create")
    @ApiOperation("创建排查计划执行任务")
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan-exe:create')")
    public CommonResult<String> createHtPlanExe(@Valid @RequestBody HtPlanExeCreateReqVO createReqVO) {
        return success(htPlanExeService.createHtPlanExe(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新排查计划执行任务")
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan-exe:update')")
    public CommonResult<Boolean> updateHtPlanExe(@Valid @RequestBody HtPlanExeUpdateReqVO updateReqVO) {
        htPlanExeService.updateHtPlanExe(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除排查计划执行任务")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan-exe:delete')")
    public CommonResult<Boolean> deleteHtPlanExe(@RequestParam("id") String id) {
        htPlanExeService.deleteHtPlanExe(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得排查计划执行任务")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan-exe:query')")
    public CommonResult<HtPlanExeRespVO> getHtPlanExe(@RequestParam("id") String id) {
        HtPlanExeDO htPlanExe = htPlanExeService.getHtPlanExe(id);
        return success(BizHtPlanExeConvert.INSTANCE.convert(htPlanExe));
    }

    @GetMapping("/list")
    @ApiOperation("获得排查计划执行任务列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan-exe:query')")
    public CommonResult<List<HtPlanExeRespVO>> getHtPlanExeList(@RequestParam("ids") Collection<String> ids) {
        List<HtPlanExeDO> list = htPlanExeService.getHtPlanExeList(ids);
        return success(BizHtPlanExeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得隐患排查计划执行分页")
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan-exe:query')")
    public CommonResult<PageResult<HtPlanExeJoinPageVO>> getHtPlanExePage(@Valid HtPlanExePageReqVO pageVO) {
        PageResult<HtPlanExeJoinPageVO> result=new PageResult<>();
        int pageNo=pageVO.getPageNo();
        pageVO.setPageNo((pageNo-1)*pageVO.getPageSize());

        //登录用户ID
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        pageVO.setCheckPeoId(userId);

        List<HtPlanExeJoinPageVO> list = htPlanExeService.selectHtPlanExePage(pageVO);
        Integer count= htPlanExeService.selectHtPlanExePageCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return success(result);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出排查计划执行任务 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:ht-plan-exe:export')")
    @OperateLog(type = EXPORT)
    public void exportHtPlanExeExcel(@Valid HtPlanExeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<HtPlanExeDO> list = htPlanExeService.getHtPlanExeList(exportReqVO);
        // 导出 Excel
        List<HtPlanExeExcelVO> datas = BizHtPlanExeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "排查计划执行任务.xls", "数据", HtPlanExeExcelVO.class, datas);
    }

}
